package com.auxgroup.smarthome.business.controller;

import com.auxgroup.bridge.business.inner.vo.AppIndexPageVo;
import com.auxgroup.bridge.business.inner.vo.CurrentAppIndexPageVo;
import com.auxgroup.smarthome.basebean.MenuTypeEnum;
import com.auxgroup.smarthome.business.service.AppIndexPageService;
import com.auxgroup.smarthome.business.service.inner.AppIndexPageInnerService;
import com.auxgroup.smarthome.business.vo.UploadIndexPageVo;
import com.auxgroup.smarthome.filter.annotation.PermissionMenu;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.SystemUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author: laiqiuhua.
 * @Date: 2017/9/8 16:04.
 */
@Api(tags = "app首页管理")
@RestController
@Validated
public class AppIndexPageController {
    @Autowired
    private AppIndexPageService appIndexPageService;
    @Autowired
    private AppIndexPageInnerService appIndexPageInnerService;

    @ApiOperation(value = "上传app首页文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uploadFile", value = "上传的文件", required = true, paramType = "form", dataType = "file"),
    })
    @PostMapping(value = "/index_page/upload")
    public ApiResponse<UploadIndexPageVo> uploadFile(@RequestParam("uploadFile") MultipartFile file) throws IOException {
        if (file == null) {
            return ApiResponse.prompt("-100", "请选择文件");
        }
        if(!file.getOriginalFilename().endsWith(".zip")) {
            return ApiResponse.prompt("-100", "请选择zip文件");
        }
        File indexFile = new File(SystemUtils.getJavaIoTmpDir(), file.getOriginalFilename());
        file.transferTo(indexFile);
        return appIndexPageService.uploadIndexPageFile(indexFile);
    }

    @ApiOperation(value = "保存app首页文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zipUri", value = "zip包地址", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "htmlUri", value = "html预览地址", required = true, paramType = "form", dataType = "string")
    })
    @PostMapping(value = "/index_page/save")
    @PermissionMenu(id="umps_appDashboard_save_file", parentId = "umps_appDashboard", name = "保存", menuType = MenuTypeEnum.MODULE)
    public ApiResponse<AppIndexPageVo> saveIndexPage(@ApiIgnore @NotBlank(message = "zipUri不能为空") String zipUri, @ApiIgnore @NotBlank(message = "htmlUri不能为空")String htmlUri)  {
        return appIndexPageInnerService.saveAppIndexPage(zipUri, htmlUri);
    }

    @ApiOperation(value = "app首页文件发布接口", notes = "首页发布使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexPageId", value = "主键", required = true, paramType = "form", dataType = "string")
    })
    @PostMapping(value = "/index_page/relese")
    @PermissionMenu(id="umps_appDashboard_release_file", parentId = "umps_appDashboard", name = "发布", menuType = MenuTypeEnum.MODULE)
    public ApiResponse<AppIndexPageVo> publish(@NotBlank(message="indexPageId不能为空") String indexPageId) {
        return appIndexPageService.publishAppIndexPage(indexPageId);
    }

    @ApiOperation(value = "app首页文件查询列表")
    @GetMapping(value = "/index_page/history")
    public ApiResponse<List<AppIndexPageVo>> findVersionHistory() {
        return appIndexPageInnerService.findVersionHistory();
    }

    @ApiOperation(value = "获取当前首页版本", notes = "用于web端")
    @GetMapping(value = "/index_page/current_web_version")
    public ApiResponse<CurrentAppIndexPageVo> getlatestIndexPageVersion() {
        return appIndexPageInnerService.getlatestIndexPageVersion();
    }

}
